/* * Proto.java
 *
 * Created on March 25, 2007, 11:54 AM
 *
 */

package fmglemmings;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.logging.*;

/**
 * A prototipus kezelesere hasznalt osztaly
 * @author ijanos
 */
public class Proto {
	/**
	 * A kimeneti naplot kezelo logger valtozo
	 */
	private static Logger logger = Logger.getLogger("fmglemmings");
	GameField gf;
	
	/**
	 * A prototipus
	 */
	public Proto() {
		loggerinit();
		gf = new GameField();
		prompt();
//		inputfile("test12.in");
	}
	
	/**
	 * A logger
	 */
	private void loggerinit(){
		try{
//			FileHandler fh = new FileHandler("lemming.log");
//			StreamHandler fh = new StreamHandler();
//			logger.setUseParentHandlers(false);
			ConsoleHandler fh = new ConsoleHandler();
			fh.setFormatter(new SimpleFormatter());
//			logger.getLogger("fmglemmings").addHandler(fh);
			logger.setLevel(Level.ALL);
		} catch (Exception e) {
			System.out.println("A logger nem indult el!\nLogger nelkul nem elet az elet...");
			System.exit(1);
		}
		logger.info("A logger elindult");
	}
	/**
	 * Fajbol olvassa a parancsokat es adja tovabb a parsernak
	 * @param infile A bementi parancsfajl utvonala
	 */
	private void inputfile(String infile){
		try {
			BufferedReader in = new BufferedReader(new FileReader(infile));
			String str;
			while ((str = in.readLine()) != null) {
				parser(str);
			}
		}catch (Exception e){
			logger.warning(e.toString());
		}
	}
	/**
	 * Kapott string parseolasa.
	 * @param str Az ertelmezendo parancs
	 */
	private void parser(String str){
		try {
			StringTokenizer parser = new StringTokenizer(str);
			String s = parser.nextToken();
			if(s.equals("init")){
				String map = parser.nextToken();
				gf = new GameField(map);
			}else if(s.equals("endtest")){
				System.exit(0);
			}else gf.input(str);
		} catch (GameOverException e){
//			logger.info("GAME OVER\n Lemmingek szama: "+e.getNoLemmings()+"\n Celba ert lemmingek: "+e.getWinners());
		} catch (Exception e){
			logger.warning(e.toString());
		}
	}
	/**
	 * A standard inputrol olvasas
	 */
	private void prompt(){
		try {
			BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
			String str = "";
			while((str = in.readLine()) != null){
				parser(str);
			}
		} catch (IOException e) {}
	}
	
	/**
	 * Itt kezdodik a program futasa
	 * @param args Parancsori parameterek. Nem hasznaljuk
	 */
/*	public static void main(String[] args) {
		Proto main = new Proto();
	}*/
}
